import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * 
 */

/**
 * @author Colorblind
 *
 */
public class ErrorHandler2 {
	private ArrayList<String> errorMessageArray = new ArrayList<String>(0);
	private ArrayList<String> levelArray = new ArrayList<String>(0);
	private ArrayList<String> codeArray = new ArrayList<String>(0);
	private ArrayList<String> numberArray = new ArrayList<String>(0);

	private ArrayList<String> errorListArray = new ArrayList<String>(0);
	private ArrayList<String> lineNumberArray = new ArrayList<String>(0);
	private ArrayList<String> customFieldArray = new ArrayList<String>(0);

	/**
	  * @description ErrorHandler2 declares a new errorhandler object
	 * @callsequence 
	 * @inparam none
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         <li>Jamie - 11/25 - added some functionality needed in linker
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public ErrorHandler2(){
		errorMessageArray = new ArrayList<String>(0);
		levelArray = new ArrayList<String>(0);
		codeArray = new ArrayList<String>(0);
		numberArray = new ArrayList<String>(0);
		errorListArray = new ArrayList<String>(0);
		lineNumberArray = new ArrayList<String>(0);
		customFieldArray = new ArrayList<String>(0);
		File errorTable = new File("LinkLoadSim" + Main.systemPathSeparator
				+ "ErrorTable2.txt");
		String errorHandlerString = FileHandler.readFile(errorTable);
		String tempString = new String();
		Scanner scanOb1 = new Scanner(errorHandlerString)
		.useDelimiter("\\s*\t\\s*");
		// without newlines, we have a string that used to be 4 columns, each
		// column separated by a tab.
		// this means that if we tab deliminate we can separate the columns into
		// their respective arrays.
		while (scanOb1.hasNext()) {
			tempString = scanOb1.next();
			numberArray.add(tempString);
			tempString = scanOb1.next();
			codeArray.add(tempString);
			tempString = scanOb1.next();
			levelArray.add(tempString);
			tempString = scanOb1.next();
			errorMessageArray.add(tempString);
		}
	}
/**
	  * @description lineHasError simply checks to see if the given line contains an error.
	 * @callsequence 
	 * @inparam int linenumber the line possibly containing errors
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
 */
	public boolean lineHasError(int lineNumber) {
		String lineNumberString = String.valueOf(lineNumber);
		if (lineNumberArray.contains(lineNumberString)) {
			return true;
		} else {
			return false;
		}
	}
	/**
		  * @description containsErrors checks to see if any errors exist in the errorhandler object
	 * @callsequence 
	 * @inparam none
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public boolean containsErrors(){
		if(lineNumberArray.size()>0){
			return true;
		}
		else{
			return false;
		}
	}

	/**
	 * 
	 *
	 * @description addToErrorList adds the error you encountered to the errorhandler object.
	 * @callsequence 
	 * @inparam lineNumber the number of the line where an error was encountered
	 * @inparam errorNumber the number in the error table of the error encountered
	 * @inparam customMessage a custom message the user wants to use in place of the default message in the table
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public void addToErrorList(String lineNumber, String errorNumber,
			String customMessage) {
		// adds an error to the list, and the line number in a corresponding
		// array.
		// the idea is we can build a list of errors as we find them
		// TODO check for these errors
		// lineNumber must be a number in the range of 0-4096
		// errorNumber must be between 1 and the highest number in the error
		// table.
		lineNumberArray.add(lineNumber);
		errorListArray.add(errorNumber);
		customFieldArray.add(customMessage);

	}
	/**
	 * 
	 *
	 * @description addToErrorList adds the error you encountered to the errorhandler object.
	 * @callsequence 
	 * @inparam lineNumber the number of the line where an error was encountered
	 * @inparam errorNumber the number in the error table of the error encountered
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public void addToErrorList(String lineNumber, String errorNumber) {
		// adds an error to the list, and the line number in a corresponding
		// array.
		// the idea is we can build a list of errors as we find them
		// TODO check for these errors
		// lineNumber must be a number in the range of 0-4096
		// errorNumber must be between 1 and the highest number in the error
		// table.
		lineNumberArray.add(lineNumber);
		errorListArray.add(errorNumber);
		customFieldArray.add(null);
	}
	/**
	 * @description removeError removes the first error encountered on a given linenumber.
	 * @callsequence 
	 * @inparam lineNumber the number of the line the error occurred on
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         <li>Jamie - 11/27 - improved functionality for custom messages
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public String removeError(int lineNumber) {
		String lineNumberString = String.valueOf(lineNumber);
		String errorString = "";
		if (lineNumberArray.contains(lineNumberString)) {
			int index = lineNumberArray.indexOf(lineNumberString);
			lineNumberString = lineNumberArray.remove(index);
			String errorNumber = errorListArray.remove(index);
			String errorMessage = errorMessageArray.get(numberArray
					.indexOf(errorNumber));
			if (index < customFieldArray.size()) {
				errorMessage = customFieldArray.remove(index);
				if(errorMessage == null){
					errorMessage = errorMessageArray.get(index);
				}
			}
			StringBuilder tempString = new StringBuilder();
			tempString.append("Line#=" + lineNumber + " ");
			tempString.append("Error#=" + errorNumber + " ");
			tempString.append(codeArray.get(numberArray.indexOf(errorNumber))
					+ " (");
			tempString.append(levelArray.get(numberArray.indexOf(errorNumber))
					+ ") ");
			tempString.append(errorMessage);
			errorString = tempString.toString();
		}
		System.out.println(errorString);
		return errorString;
	}
	/**
	 * @description removeFirstError removes the first error encountered from the first line an error occurred on
	 * @callsequence 
	 * @inparam none
	 * @outparam none
	 * @errorstested See JUnit test cases
	 * @errorsgenerated See errata report
	 * @author Jamie Trefzger
	 * @createdate 11/19/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 *         <li>Jamie - 11/19 - Created
	 *         <li>Jamie - 11/27 - improved functionality for custom messages
	 *         </ul>
	 *         <p>
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public String removeFirstError() {
		// the idea behind this is that once we output an error, we will no
		// longer
		// be interested in what it was. Thus, we can remove them as we output
		// them
		String tempString = new String();
		String errorNumber = new String();

		// make sure there is an item to remove before we try to remove
		// something.
		if (lineNumberArray.size() > 0) {
			lineNumberArray.remove(0);
			errorNumber = errorListArray.remove(0);
			//tempString = "Line#=" + lineNumber + " ";
			tempString = tempString + "Error#=" + errorNumber + " ";
			tempString = tempString
			+ codeArray.get(numberArray.indexOf(errorNumber)) + " (";
			tempString = tempString
			+ levelArray.get(numberArray.indexOf(errorNumber)) + ") ";
			// if we did not pass a custom message, return the default message
			// in the table
			if (customFieldArray.size() != 0) {
				if (customFieldArray.get(0) == null) {
					tempString = tempString
					+ errorMessageArray.get(numberArray
							.indexOf(errorNumber));
					customFieldArray.remove(0);
				}
				// if we did pass a custom message, substitute that in for the
				// default message.
				else {
					tempString = tempString + customFieldArray.remove(0);
				}
			}
			System.out.println(tempString);
			return tempString;
		} else {
			return "No remaining errors to remove from the list.";
		}
	}
}
